﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class frmChuyenDin : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmChuyenDin()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;

                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;

                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);


                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
                //txtChuHo.Text = list_nk.Where(item => item.QuanHe == "Chủ hộ").FirstOrDefault().HoTen;

                dgvNhanKhau.DataSource = list_nk;
                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;            }
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.ForeColor = Color.AliceBlue;
                style.BackColor = Color.Red;
                for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
                {
                    if ((int)dgvNhanKhau.Rows[i].Cells[12].Value != 0)
                    {
                        //MessageBox.Show("geg");
                        dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                    }
                    //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                    //{
                    //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                    //}

                }

            }
        }
        public int MaNK;

        private void dgvNhanKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;

            foreach (DataGridViewCell select in dgvNhanKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;

                MaNK = (int)row.Cells[0].Value;
                txtHoTen.Text = (string)row.Cells[1].Value;
                txtCMND.Text = (string)row.Cells[4].Value;
                dateNgaySinh.Text = Convert.ToDateTime(row.Cells[5].Value).ToShortDateString();
                    // ((DateTime)row.Cells[5].Value).ToString("dd-MM-yyyy");
                if ((bool)row.Cells[7].Value == true)
                    rdtGioiTinhNam.Checked = true;
                else
                    rdtGioiTinhNu.Checked = true;
                txtQueQuan.Text = (string)row.Cells[8].Value;
                txtDanToc.Text = (DanTocBLL.GetDanTocByMaDT((byte)row.Cells[9].Value).TenDanToc).ToString();

                txtNgheNghiep.Text = (string)row.Cells[10].Value;
                txtQuocTich.Text = (QuocTichBLL.GetQuocTichByMaQT((byte)row.Cells[11].Value).TenQuocTich).ToString();
                txtTonGiao.Text = (TonGiaoBLL.GetTonGiaoByMaTG((byte)row.Cells[13].Value).TenTonGiao).ToString();
                txtTrinhDo.Text = (TrinhDoBLL.GetTrinhDoByMaTD((byte)row.Cells[14].Value).TenTrinhDo).ToString();

                //txtMaCT.Text = ((int)row.Cells[0].Value).ToString();
                //txtMaCT.Enabled = false;

                if ((int)row.Cells[2].Value == 0)
                {
                    return;
                }
                else
                {
                    KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhByMaKS((int)row.Cells[2].Value);
                    txtNoiSinh.Text = ks.NoiSinh;
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            //string tl= ThietLapBLL.LoadDiaChi();
            //ChuyenKhauPublic ck = new ChuyenKhauPublic();
            //ck.MaNK = int.Parse(txtMaNK.Text);
            //ck.SoChuyenKhau=int.Parse(txtSoChuyenKhau.Text);
            ////MessageBox.Show(tl);
            //ck.LyDoChuyen=txtLyDoChuyen.Text;
            //ck.MaCK=int.Parse(txtMaCK.Text);
            ////ck.NgayChuyen=dateNgayChuyen.Value;
            //ck.NoiChuyen=cmbPhuongXa.Text+", "+cmbQuanHuyen.Text+", "+cmbTinh.Text;

            //ck.NoiCap=ThietLapBLL.LoadDiaChi();
            //ck.CanBoNoiChuyen = cmbCanBoDangKi.Text;
            //ck.CanBoNoiNhan = "";
            //ck.ChucVuNguoiChuyen = txtChucVuNguoiChuyen.Text;
            //ck.ChucVuNguoiNhan = "";
            //ck.LyDoChuyen = txtLyDoChuyen.Text;
            //ck.NguoiNhan = "";
            //ck.NgayCap = dateNgayCap.Value;
            //ck.MaCK = int.Parse(txtMaCK.Text);
            //ck.NgayNhan = null;
            //ck.ChuHoNoiDi = txtChuHo.Text;
            //ck.QuanHe = txtQuanHe.Text;
            //ChuyenKhauBLL.InsertChuyenKhau(ck);
            ////ck.NgayChuyen
            
        }

        private void frmChuyenDin_Load(object sender, EventArgs e)
        {
            
            groupBox2.Enabled = false;
            //txtMaCK.Text = DungChung.LoadMaCDi().ToString();

            cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen;
            cmbNguoiCap.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";
            //load chuc vu
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiChuyen.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiChuyen.Enabled = false;

            dateNgayCap.Value = DateTime.Parse(DateTime.Now.ToString());

            cmbTinh.DataSource = TinhBLL.LoadTinh();
            cmbTinh.DisplayMember = "TenTinh";
            cmbTinh.ValueMember = "MaT";

            cmbTinh.SelectedIndex = -1;
            cmbTinh.Text = "   --- Chọn Tỉnh ---   ";
            cmbQuanHuyen.Text = "  --- Chọn Huyện ---  ";
            cmbPhuongXa.Text = "      --- Chọn Xã ---   ";
            txtSoChuyenKhau.Text = DungChung.LoadMaCDi().ToString();
            txtNoiCap.Text = ThietLapBLL.LoadDiaChi();
            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";
        }

        private void cmbTinh_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //TinhPublic t = TinhBLL.LoadQuanHuyenByMaQH((byte)cmbQuanHuyen.SelectedValue);
            cmbQuanHuyen.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((byte)cmbTinh.SelectedValue);
            cmbQuanHuyen.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyen.ValueMember = "MaQH";
        }

        private void cmbQuanHuyen_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //QuanHuyenPublic px = QuanHuyenBLL.LoadQuanHuyenByMaQH((byte)cmbQuanHuyen.SelectedValue);
            cmbPhuongXa.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((byte)cmbQuanHuyen.SelectedValue);
            cmbPhuongXa.DisplayMember = "TenPhuongXa";
            cmbPhuongXa.ValueMember = "MaPX";
        }

        private void btnChuyen_Click(object sender, EventArgs e)
        {
            if (txtLyDoChuyen.Text == "")
            {
                MessageBox.Show("Chưa nhập lí do chuyển ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtChuHoMoi.Text == "")
            {
                MessageBox.Show("Chưa nhập chủ hộ nơi chuyển ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            //ck.MaNK = int.Parse(txtMaNK.Text);
            ck.MaNK = MaNK;
            ck.SoChuyenKhau = int.Parse(txtSoChuyenKhau.Text);
            //MessageBox.Show(tl);
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            ck.MaCK = DungChung.LoadMaCDi();
            //ck.NgayChuyen=dateNgayChuyen.Value;
            ck.NoiChuyen = cmbPhuongXa.Text + ", " + cmbQuanHuyen.Text + ", " + cmbTinh.Text;

            ck.NoiCap = int.Parse(ThietLapBLL.LoadMaDiaChi());
            ck.CanBoNoiChuyen = cmbCanBoDangKi.Text;
            ck.CanBoNoiNhan = "";
            ck.ChucVuNguoiChuyen = txtChucVuNguoiChuyen.Text;
            ck.ChucVuNguoiNhan = "";
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            ck.NguoiNhan = "";
            ck.NgayCap = dateNgayCap.Value;
            ck.NguoiCap = cmbNguoiCap.Text;
            ck.NgayNhan = null;
            //ck.ChuHoNoiDi = txtChuHoMoi.Text;
            //ck.MaQH = (int)cmbQuanHe.SelectedValue;
            ChuyenKhauBLL.InsertChuyenKhau(ck);
            dgvChuyenDi.DataSource = ChuyenKhauBLL.LoadChuyenKhauBySoCK(int.Parse(txtSoChuyenKhau.Text));
            cmbTinh.Enabled = false;
            cmbQuanHuyen.Enabled = false;
            cmbPhuongXa.Enabled = false;
            txtChuHoMoi.Enabled = false;

        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbNguoiChuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
     
    }
}

